package com.lihaogn.sell.web;

import com.lihaogn.sell.entity.FoodCookCategory;
import com.lihaogn.sell.entity.FoodMaterial;
import com.lihaogn.sell.entity.FoodTypeCategory;
import com.lihaogn.sell.service.ProductCategoryService;
import com.lihaogn.sell.vo.ListVo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

/**
 * @FileName: ProductCategoryController
 * @Description: TODO
 * @Author: lihao
 * @Date: 2019-05-14 10:47
 * @Version: 1.0
 */
@Controller
@Api(tags = "菜品分类数据接口")
public class ProductCategoryController {

    @Autowired
    private ProductCategoryService productCategoryService;

    @Autowired
    private ListVo listVo;

    private String cookCategoryTempId;

    private String typeCategoryTempId;

    private int foodMaterialTempId;

    // ======================= 访问页面 =======================

    @RequestMapping(value = "/foodMaterialAdd")
    private String foodMaterialAdd() {
        return "product/foodMaterial-add";
    }

    @ApiIgnore
    @RequestMapping(value = "/typeCategoryAdd")
    private String typeCategoryAdd() {
        return "product/typeCategory-add";
    }

    @ApiIgnore
    @RequestMapping(value = "/cookCategoryShow")
    private String cookCategoryShow() {
        return "product/foodCookCategory-list";
    }

    @ApiIgnore
    @RequestMapping(value = "/typeCategoryShow")
    private String typeCategoryShow() {
        return "product/foodTypeCategory-list";
    }

    @ApiIgnore
    @RequestMapping(value = "/materialShow")
    private String materialShow() {
        return "product/foodMaterial-list";
    }

    @ApiIgnore
    @RequestMapping(value = "/cookCategoryAdd")
    private String categoryAdd() {
        return "product/cookCategory-add";
    }

    @ApiIgnore
    @RequestMapping(value = "/cookCategoryEdit")
    private String cookCategoryEdit(String cookCategoryId) {

        cookCategoryTempId = cookCategoryId;
        return "product/cookCategory-edit";
    }

    @ApiIgnore
    @RequestMapping(value = "/typeCategoryEdit")
    private String typeCategoryEdit(String typeCategoryId) {

        typeCategoryTempId = typeCategoryId;
        return "product/typeCategory-edit";
    }

    @RequestMapping(value = "/foodMaterialEdit")
    private String foodMaterialEdit(int materialId) {
        foodMaterialTempId = materialId;
        return "product/foodMaterial-edit";
    }

    // ======================= 操作 =======================

    // ========== 查询 ==========

    @GetMapping(value = "/getAllFoodCookCategory")
    @ResponseBody
    private List<FoodCookCategory> getAllFoodCookCategory() {
        List<FoodCookCategory> allFoodCookCategory = productCategoryService.getAllFoodCookCategory();
        return allFoodCookCategory;
    }

    @GetMapping(value = "/getAllFoodTypeCategory")
    @ResponseBody
    private List<FoodTypeCategory> getAllFoodTypeCategory() {
        List<FoodTypeCategory> foodTypeCategoryList = productCategoryService.getAllFoodTypeCategory();
        return foodTypeCategoryList;
    }

    /**
     * 获取需要修改的食材
     *
     * @return
     */
    @GetMapping(value = "/getFoodMaterialForEdit")
    @ResponseBody
    private FoodMaterial getFoodMaterialForEdit() {
        return productCategoryService.queryFoodMaterialById(foodMaterialTempId);
    }

    /**
     * 获取要修改的typeCategory
     *
     * @return
     */
    @GetMapping(value = "/getFoodTypeCategoryForEdit")
    @ResponseBody
    private FoodTypeCategory getFoodTypeCategoryForEdit() {

        return productCategoryService.queryFoodTypeCategoryById(typeCategoryTempId);
    }

    /**
     * 获取要修改的cookCategory
     *
     * @return
     */
    @GetMapping(value = "/getFoodCookCategoryForEdit")
    @ResponseBody
    private FoodCookCategory getFoodCookCategoryForEdit() {

        return productCategoryService.queryFoodCookCategoryById(cookCategoryTempId);
    }

    /**
     * 查询食材名称是否存在
     *
     * @param name 食材名称
     * @return 查到的数目
     */
    @GetMapping(value = "/queryExistMaterialName")
    @ResponseBody
    private Integer queryExistMaterialName(String name) {
        return productCategoryService.queryExistMaterialName(name);
    }

    /**
     * 查询cook分类名是否已经存在
     *
     * @param name
     * @return
     */
    @GetMapping(value = "/queryExistCookName")
    @ResponseBody
    private Integer queryExistCookName(String name) {

        return productCategoryService.queryExistCookName(name);
    }

    /**
     * 查询type分类名是否已经存在
     *
     * @param name
     * @return
     */
    @GetMapping(value = "/queryExistTypeName")
    @ResponseBody
    private Integer queryExistTypeName(String name) {

        return productCategoryService.queryExistTypeName(name);
    }

    /**
     * 展示所有的cook分类
     *
     * @return
     */
    @GetMapping(value = "/showFoodCookCategory")
    @ResponseBody
    private ListVo showFoodCookCategory() {

        List<FoodCookCategory> foodCookCategoryList = productCategoryService.getAllFoodCookCategory();

        listVo.setCode(0);
        listVo.setMsg("success");
        listVo.setCount(foodCookCategoryList.size());
        listVo.setData(foodCookCategoryList);

        return listVo;
    }

    @GetMapping(value = "/showFoodTypeCategory")
    @ResponseBody
    private ListVo showFoodTypeCategory() {

        List<FoodTypeCategory> foodTypeCategoryList = productCategoryService.getAllFoodTypeCategory();

        listVo.setCode(0);
        listVo.setMsg("success");
        listVo.setCount(foodTypeCategoryList.size());
        listVo.setData(foodTypeCategoryList);

        return listVo;
    }

    /**
     * 查询所有食材信息
     *
     * @return
     */
    @GetMapping(value = "/showFoodMaterial")
    @ResponseBody
    private ListVo getAllFoodMaterial() {
        List<FoodMaterial> foodMaterialList = productCategoryService.getAllFoodMaterial();

        listVo.setCode(0);
        listVo.setMsg("success");
        listVo.setCount(foodMaterialList.size());
        listVo.setData(foodMaterialList);

        return listVo;
    }

    // ========== 添加 ==========

    /**
     * 添加一个烹饪类别
     *
     * @param foodCookCategory
     * @return
     */
    @PostMapping(value = "/addFoodCookCategory")
    @ResponseBody
    private Integer addFoodCookCategory(FoodCookCategory foodCookCategory) {

        foodCookCategory.setCategoryId(UUID.randomUUID().toString());
        int i = productCategoryService.addOneFoodCookCategory(foodCookCategory);

        return i;

    }

    /**
     * 添加一个荤素类别
     *
     * @param foodTypeCategory
     * @return
     */
    @PostMapping(value = "/addFoodTypeCategory")
    @ResponseBody
    private Integer addFoodTypeCategory(FoodTypeCategory foodTypeCategory) {

        foodTypeCategory.setCategoryId(UUID.randomUUID().toString());
        int i = productCategoryService.addOneFoodTypeCategory(foodTypeCategory);

        return i;

    }

    /**
     * 添加一个食材
     *
     * @param foodMaterial 食材对象
     * @return 记录修改条数
     */
    @PostMapping(value = "/addFoodMaterial")
    @ResponseBody
    private Integer addFoodMaterial(FoodMaterial foodMaterial) {
        int i = productCategoryService.addFoodMaterial(foodMaterial);
        return i;
    }

    // ========== 更新 ==========

    /**
     * 更新cook分类
     *
     * @param foodTypeCategory
     * @return
     */
    @PutMapping(value = "/updateOneFoodTypeCategory")
    @ResponseBody
    private Integer updateOneFoodTypeCategory(FoodTypeCategory foodTypeCategory) {
        return productCategoryService.updateOneFoodTypeCategory(foodTypeCategory);
    }

    /**
     * 更新type分类
     *
     * @param foodCookCategory
     * @return
     */
    @PutMapping(value = "/updateOneFoodCookCategory")
    @ResponseBody
    private Integer updateOneFoodCookCategory(FoodCookCategory foodCookCategory) {
        return productCategoryService.updateOneFoodCookCategory(foodCookCategory);
    }

    /**
     * 更新食材名称
     *
     * @param foodMaterial
     * @return
     */
    @PutMapping(value = "/updateFoodMaterial")
    @ResponseBody
    private Integer updateFoodMaterial(FoodMaterial foodMaterial) {

        return productCategoryService.updateFoodMaterial(foodMaterial);

    }

    // ========== 删除 ==========

    /**
     * 删除单个食材记录
     * @param materialId
     * @return
     */
    @DeleteMapping(value = "/deleteOneFoodMaterial")
    @ResponseBody
    private Integer deleteOneFoodMaterial(int materialId) {
        return productCategoryService.deleteOneFoodMaterial(materialId);
    }

    /**
     * 删除一个type分类
     *
     * @param categoryId
     * @return
     */
    @DeleteMapping(value = "/deleteOneFoodTypeCategory")
    @ResponseBody
    private Integer deleteOneFoodTypeCategory(String categoryId) {
        return productCategoryService.deleteOneFoodTypeCategory(categoryId);
    }

    @DeleteMapping(value = "/deleteOneFoodCookCategory")
    @ResponseBody
    private Integer deleteOneFoodCookCategory(String categoryId) {
        return productCategoryService.deleteOneFoodCookCategory(categoryId);
    }

    @DeleteMapping(value = "/deleteMultiFoodCookCategory")
    @ResponseBody
    private Integer deleteMultiFoodCookCategory(HttpServletRequest request) {
        return productCategoryService.deleteMultiFoodCookCategory(request);
    }

    @DeleteMapping(value = "/deleteMultiFoodTypeCategory")
    @ResponseBody
    private Integer deleteMultiFoodTypeCategory(HttpServletRequest request) {
        return productCategoryService.deleteMultiFoodTypeCategory(request);
    }

    @DeleteMapping(value = "/deleteMultiFoodMaterial")
    @ResponseBody
    private Integer deleteMultiFoodMaterial(HttpServletRequest request) {
        return productCategoryService.deleteMultiFoodMaterial(request);
    }

}
